package com.live.luowang.Fragment.Forum;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.live.luowang.Activity.InnerActivity.FormHotInfoActivity;
import com.live.luowang.Adapter.ForumHotListViewAdapter;
import com.live.luowang.Function.FunctionClass;
import com.live.luowang.Function.HttpUrl;
import com.live.luowang.JsonData.ForumBannerData;
import com.live.luowang.JsonData.ForumData;
import com.live.luowang.MyApplication;
import com.live.luowang.R;

/**
 * Created by Chen on 2016/4/1.
 */
public class ForumHotFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
    private ForumData forumData;
    private ForumBannerData forumBannerData;
    private ImageView loading;
    private AnimationDrawable animationDrawable;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.forum_fragment_hot_layout,null);
        listView= (ListView) view.findViewById(R.id.ForumHotListView);
        loading= (ImageView) view.findViewById(R.id.Loading);
        animationDrawable= (AnimationDrawable) loading.getDrawable();
        animationDrawable.start();
        swipeRefreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.ForumHotSwipeRefresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.titleText);
        swipeRefreshLayout.setOnRefreshListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), FormHotInfoActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("data",forumData.getData().getItems().get(position-1));
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });
        onRefresh();
        return view;
    }
    @Override
    public void onRefresh() {
        getBannerData();
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    listView.setAdapter(new ForumHotListViewAdapter(getActivity(),forumData.getData().getItems()));
                    animationDrawable.stop();
                    loading.setVisibility(View.GONE);
                    swipeRefreshLayout.setRefreshing(false);
                    break;
                case 2:
                    if(listView.getHeaderViewsCount()!=1){
                        listView.addHeaderView(FunctionClass.getForumHotHead(getActivity(),forumBannerData));
                    }
                    getData();
                    break;
            }
        }
    };

    private void getData(){
        final StringRequest request=new StringRequest(0, HttpUrl.FORUMHOT, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                forumData =gson.fromJson(response,ForumData.class);
                handler.sendEmptyMessage(1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyApplication.getInstance().getQueue().add(request);
    }
    private void getBannerData(){
        final StringRequest request=new StringRequest(0, HttpUrl.FORUMBANNER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                forumBannerData =gson.fromJson(response,ForumBannerData.class);
                handler.sendEmptyMessage(2);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyApplication.getInstance().getQueue().add(request);
    }
}
